import fetch from '../config/fetch'
import { Toast,Indicator } from 'mint-ui';

export const bhlist = (param) => fetch('/wapsjson/bhlist.json',param);
export const bhskus = (param) => fetch('/wapsjson/bhskus.json',param);
export const hasstock = (param) => fetch('/wapsjson/hasstock.json',param);
export const nostock = (param) => fetch('/wapsjson/nostock.json',param);
export const msgcontent = (param) => fetch('/wapsjson/msgcontent.json',param);
export const skucount = (param) => fetch('/wapsjson/skucount.json',param);
export const allNoStock = (param) => fetch('/wapsjson/allNoStock.json',param);
export const allHasStock = (param) => fetch('/wapsjson/allHasStock.json',param);
export const pideal = (param) => fetch('/wapsjson/pideal.json',param);

export const ajax = {
    data(){
      return {
        loadingTool:'',
        _imdoing:'',
        myToast:''
      }
    },
    methods: {
        async doajax(method,param,success,err){
          if (this._imdoing==method) {
            return;
          }
          this._imdoing=method;
          this.openLoading();
          this.ajax(await method(param),success,err);
          this.closeLoading();
          this._imdoing='';
        },
        ajax(res, success,err){
            if(res.success){
                success.call(this, res)
            }else{
                if(err!==undefined){
                    err.call(this, res);
                }else{
                    this.error(res.msg);
                }
            }

        },
      openLoading(){
        Indicator.open();
      },
      closeLoading(){
        Indicator.close();
      },
      success(message){
        if (this.myToast != ''&&this.myToast.visible) {
          this.myToast.close();
        }
        this.myToast=Toast(message);
      },
      error(message){
        if (this.myToast != ''&&this.myToast.visible) {
          this.myToast.close();
        }
        this.myToast=Toast(message);
      },
      warn(message){
        if (this.myToast != ''&&this.myToast.visible) {
          this.myToast.close();
        }
        this.myToast=Toast(message);
      }
    }
}
